package com.czx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czx.entity.Message;
import com.czx.mapper.MessageMapper;
import com.czx.service.SysMessageService;
import com.czx.vo.FollowerVo;
import com.czx.vo.NoticeVo;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SysMessageImpl extends ServiceImpl<MessageMapper, Message> implements SysMessageService {

    /**
     * 用户总共的未读消息
     * @param id
     * @return
     */
    @Override
    public Integer SysMessage(Integer id){
        Integer integer = baseMapper.SysUnreadMessage(id);
        return integer;
    }


    /**
     * 查询最新一条消息的数据
     * @param id
     * @param type
     * @return
     */
    @Override
    public NoticeVo getNotice(Integer id,Integer type){
        NoticeVo newNotice = getNewNotice(id, type);
        if (newNotice == null) {
            return newNotice;
        }
        Integer count = getCount(id, type);
        Integer unreadCount = getUnreadCount(id, type);
        newNotice.setCount(count);
        newNotice.setUnread(unreadCount);
        System.out.println(newNotice);
        return newNotice;
    }

    /**
     * 查询消息总数
     * @param id
     * @param type
     * @return
     */
    public Integer getCount(Integer id,Integer type){
        Integer count = baseMapper.getCount(id, type);
        return count;
    }

    /**
     * 增加一条message
     * @param followerVo
     */
    @Override
    public void addFollow(FollowerVo followerVo) {
        Message message =new Message();
        message.setFromId(followerVo.getUserId());
        message.setToId(followerVo.getFollowerId());
        message.setType(2);
        message.setCreateTime(new Date());
        message.setStatus(0);
        baseMapper.insert(message);
    }

    /**
     * 查询未读消息数量
     * @param id
     * @param type
     * @return
     */
    @Override
    public Integer getUnreadCount(Integer id,Integer type){
        Integer unreadCount = baseMapper.getUnreadCount(id, type);
        return unreadCount;
    }


    /**
     * 查询所有类型的未读消息数量
     * @param id
     * @param type
     * @return
     */
    @Override
    public NoticeVo getNewNotice(Integer id,Integer type){
        NoticeVo newNotice = baseMapper.getNewNotice(id, type);
        return newNotice;
    }


}
